package ${pPackage}.service.impl;

import javax.annotation.Resource;
import ${pPackage}.service.${ClassName}Service;
import ${pPackage}.vo.resp.RespDataVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import ${pPackage}.vo.${ClassName}VO;
import ${pPackage}.mapper.${ClassName}Mapper;
import ${pPackage}.model.${ClassName}DO;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @Description ${ClassName}ServiceImpl
 * @Author ${author}
 * @Date ${createDate}
 */
@Service
public class ${ClassName}ServiceImpl implements ${ClassName}Service {

    @Resource
    private ${ClassName}Mapper ${ClassName?uncap_first}Mapper;

    @Override
    public Boolean create(${ClassName}VO ${ClassName?uncap_first}VO) {
        BeanCopier beanCopier = BeanCopier.create(${ClassName}VO.class,${ClassName}DO.class,false);
        ${ClassName}DO ${ClassName?uncap_first}DO = new ${ClassName}DO();
        beanCopier.copy(${ClassName?uncap_first}VO, ${ClassName?uncap_first}DO, null);
        int count = ${ClassName?uncap_first}Mapper.insertSelective(${ClassName?uncap_first}DO);
        return count>0;
    }

    @Override
    public Boolean deleteById(${table.primaryKeyType} ${table.primaryKeyExt}) {
        int count = ${ClassName?uncap_first}Mapper.deleteByPrimaryKey(${table.primaryKeyExt});
        return count>0;
    }

    @Override
    public Boolean batchDeleteByIds(List<Long> ids) {
        return  ${ClassName?uncap_first}Mapper.batchDeleteByIds(ids)>0;
    }

    @Override
    public Boolean update(${ClassName}VO ${ClassName?uncap_first}VO) {
        BeanCopier beanCopier = BeanCopier.create(${ClassName}VO.class,${ClassName}DO.class,false);
        ${ClassName}DO ${ClassName?uncap_first}DO = new ${ClassName}DO();
        beanCopier.copy(${ClassName?uncap_first}VO, ${ClassName?uncap_first}DO, null);
        int count = ${ClassName?uncap_first}Mapper.updateByPrimaryKeySelective(${ClassName?uncap_first}DO);
        return count>0;
    }

    @Override
    public ${ClassName}VO get${ClassName}InfoById(${table.primaryKeyType} ${table.primaryKeyExt}) {
        ${ClassName}DO ${ClassName?uncap_first}DO = ${ClassName?uncap_first}Mapper.selectByPrimaryKey(${table.primaryKeyExt});
        if(null!=${ClassName?uncap_first}DO){
            BeanCopier beanCopier = BeanCopier.create(${ClassName}DO.class,${ClassName}VO.class,false);
            ${ClassName}VO ${ClassName?uncap_first}VO = new ${ClassName}VO();
            beanCopier.copy(${ClassName?uncap_first}DO, ${ClassName?uncap_first}VO, null);
            return ${ClassName?uncap_first}VO;
        }
        return null;
    }

    @Override
    public RespDataVO<${ClassName}VO> get${ClassName}PageInfo(Integer page,Integer limit,String id) {
        if (null==page){
            page = 1;
        }
        if (null==limit){
            limit = 20;
        }
        PageHelper.startPage(page, limit);
        List<${ClassName}DO> ${ClassName?uncap_first}DOS = ${ClassName?uncap_first}Mapper.selectList(StringUtils.isEmpty(id) ? null : Long.valueOf(id));
        PageInfo<${ClassName}DO> ${ClassName?uncap_first}DOPageInfo = new PageInfo<>(${ClassName?uncap_first}DOS);
        List<${ClassName}DO> list = ${ClassName?uncap_first}DOPageInfo.getList();
        RespDataVO<${ClassName}VO> respDataVO = new RespDataVO<${ClassName}VO>();
        respDataVO.setCode(0);
        respDataVO.setCount(${ClassName?uncap_first}DOPageInfo.getTotal());
        if (!CollectionUtils.isEmpty(list)){
            List<${ClassName}VO> ${ClassName?uncap_first}VOS = list.stream().map(${ClassName?uncap_first}DO -> {
                BeanCopier beanCopier = BeanCopier.create(${ClassName}DO.class, ${ClassName}VO.class, false);
                ${ClassName}VO ${ClassName?uncap_first}VO = new ${ClassName}VO();
                beanCopier.copy(${ClassName?uncap_first}DO, ${ClassName?uncap_first}VO, null);
                return ${ClassName?uncap_first}VO;
            }).collect(Collectors.toList());
            respDataVO.setData(${ClassName?uncap_first}VOS);
            return respDataVO;
        }
        respDataVO.setData(null);
        return respDataVO;
    }

}